﻿#include "licenseManager.h"

LicenseManager::LicenseManager()
{
    _crypto = new Crypto();
    _timer = new QTimer();
    _settings = new QSettings(_organization, _application);

    connect(_timer, SIGNAL(timeout()), this, SLOT(updateTimeStamp()));
    _timer->setInterval(1000);     // 每秒更新一次时间
    _timer->start();

}

LicenseManager::~LicenseManager(){

    if(_timer){
        _timer->stop();
        delete _timer;
    }

    if(_crypto){
        delete _crypto;
    }
    if(_settings){
        delete _settings;
    }

}


LicenseManager& LicenseManager::I(){
    static LicenseManager instance;
    return instance;
}


void LicenseManager::updateTimeStamp(){


    // 检测当前License状态，无License时不需要更新时间戳
    if(!checkSystemLicense()){
        return;
    }

    QString curTime = QDateTime::currentDateTime().toString("yyyy-MM-dd");

    // 第一次记录时间戳
    if(!_settings->contains("license_timestamp")){
        _settings->setValue("license_timestamp", curTime);
        return;
    }

    // 更新时间戳
    QString lastTime = _settings->value("license_timestamp").value<QString>();
    if(lastTime > curTime){
        emit showTimeExceptionPage();
    }else{
        _settings->setValue("license_timestamp", curTime);
    }

    // 减少调用次数
    _timer->setInterval(_timer->interval() + 1000);

}

void LicenseManager::clearSystemLicense(){
    _settings->setValue("license", "");
    _settings->setValue("license_time", "");
    checkSystemLicense();
}


bool LicenseManager::checkSystemLicense(){


    bool res = checkLicense(getSystemLicense());

    if(res){
//        qDebug() << "CheckSystemLicense - true";
        return true;
    }else{
//        qDebug() << "CheckSystemLicense - false";
        emit showLicensePage();
        return false;
    }

}

bool LicenseManager::checkLicense(QString cipherText){

    QString date = license2Info(cipherText)["LimitDate"].value<QString>();
    if(date!="" && date >= QDateTime::currentDateTime().toString("yyyy-MM-dd")){
        return true;
    }else{
        return false;
    }
}

QString LicenseManager::getUUID(){

    QProcess p;
    QString cmd = "wmic csproduct get uuid";

    p.start(cmd);
    p.waitForFinished(-1);

    QString result = QString::fromLocal8Bit(p.readAllStandardOutput());
    QStringList list = cmd.split(" ");
    result = result.remove(list.last(), Qt::CaseInsensitive);
    result = result.replace("\r", "");
    result = result.replace("\n", "");
    result = result.simplified();
    p.close();
    return result;

}


bool LicenseManager::registSystemLicense(QString cipherText){

    if(checkLicense(cipherText)){
        _settings->setValue("license", cipherText);
        return true;
    }else{
        return false;
    }

}

QString LicenseManager::getSystemLicense(){
    if(_settings->contains("license")){
        return _settings->value("license").value<QString>();
    }else{
        return "";
    }
}

QVariantMap LicenseManager::getSystemLicenseInfo(){
    return license2Info(getSystemLicense());
}


QVariantMap LicenseManager::license2Info(QString cipherText){


    QVariantMap res;

    res["UserName"] = "";
    res["LimitDate"] = "";

    QString plainText = _crypto->cipher2Plain(cipherText);
    QString spaceCharacter = "[SPACECHARACTER]";


    QStringList infoList = plainText.split(spaceCharacter);

    if(infoList.size() == 4 && infoList[1] == getUUID()){
        res["UserName"] = infoList[0];
        res["LimitDate"] = infoList[2];
    }
    return res;
}




